package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("HazardController")
public class HazardController extends CmiEmaController {

	@RequestMapping("/listHazards")
	public ModelAndView listHazards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = hazardPaginationService.updatePagination(request, pagination);
		Set<Hazard> hazards = hazardPaginationService.getPaginationHazards(pagination);

		mav.addObject("hazards", hazards);
		mav.addObject("entityName", "hazard");
		mav.addObject("pagination", pagination);
		mav.setViewName("hazard/listHazards.jsp");
		return mav;
	}

	public String listHazards() {
		return "redirect:/listHazards";
	}

	@RequestMapping("/viewHazard")
	public ModelAndView viewHazard(@RequestParam Integer hazardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("hazard", hazardDAO.findHazardByPrimaryKey(hazardId));
		mav.setViewName("hazard/viewHazard.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteHazards")
	public ModelAndView confirmDeleteHazards(
			@RequestParam(value = "deleteHazardsIds", required = true) Set<Integer> deleteHazardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Hazard> deleteHazards = hazardDAO.findHazardsByIds(deleteHazardsIds);
		Set<Hazard> referencedHazards = hazardService.loadReferenceHazards(deleteHazards);
		boolean hasReferenced;
		if (referencedHazards.size() == 0) {
			mav.addObject("deleteEntitys", deleteHazards);
			hasReferenced = false;
		} else {
			deleteHazards.removeAll(referencedHazards);
			mav.addObject("referencedEntitys", referencedHazards);
			mav.addObject("deleteEntitys", deleteHazards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Hazard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listHazards");
		return mav;
	}

	@RequestMapping("/deleteHazard")
	public String deleteHazard(@RequestParam Integer hazardId) {
		Hazard hazard = hazardDAO.findHazardByPrimaryKey(hazardId);
		hazardService.deleteHazard(hazard);
		return "forward:/listHazards";
	}

	@RequestMapping("/confirmDeleteHazard")
	public ModelAndView confirmDeleteHazard(@RequestParam Integer hazardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("hazard", hazardDAO.findHazardByPrimaryKey(hazardId));

		mav.setViewName("hazard/deleteHazard.jsp");

		return mav;
	}

	@RequestMapping("/deleteHazards")
	public String deleteHazards(
			@RequestParam(value = "deleteHazardsId", required = true) Set<Integer> deleteHazardsId) {
		hazardService.deleteHazards(deleteHazardsId);
		return "forward:/listHazards";
	}

	@RequestMapping("/editHazard")
	public ModelAndView editHazard(@RequestParam Integer hazardId) {

		ModelAndView mav = new ModelAndView();

		Hazard hazard = hazardDAO.findHazardByPrimaryKey(hazardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("hazard", hazard);

		// 把所有的引用hazard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(hazardService.loadHazardObjectReferencedSet(hazard));

		// 将编辑页面hazard添加到mav中
		mav.setViewName("hazard/editHazard.jsp");
		return mav;
	}

	@RequestMapping("/newHazard")
	public ModelAndView newHazard() {
		ModelAndView mav = new ModelAndView();
		Hazard hazard = new Hazard();

		mav.addObject("hazard", hazard);

		// 把所有的引用hazard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(hazardService.loadHazardObjectReferencedSet(hazard));

		mav.addObject("newFlag", true);
		mav.setViewName("hazard/editHazard.jsp");

		return mav;
	}

	@RequestMapping("/saveHazard")
	public ModelAndView saveHazard(@ModelAttribute Hazard hazard) {

		ModelAndView mav = new ModelAndView();

		Hazard existingHazard = CmiHibernateProxyUtil
				.deproxy(hazardDAO.findHazardByPhysicalPrimaryKey(hazard.getName()));

		// 新建记录
		if (hazard.getId() == null) {
			if (hazard.equals(existingHazard)) {

				mav.addAllObjects(hazardService.loadHazardClassReferencedSet(hazard));
				// mav = editContinue(mav, hazard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("hazard/editHazard.jsp");
			} else {
				hazardService.saveHazard(hazard);
				mav.setViewName("forward:/listHazards");
			}
		}
		// 编辑记录
		else {
			Hazard originHazard = CmiHibernateProxyUtil.deproxy(hazardDAO.findHazardByPrimaryKey(hazard.getId()));
			if (!hazard.equals(originHazard) && hazard.equals(existingHazard)) {
				mav.addAllObjects(hazardService.loadHazardClassReferencedSet(hazard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("hazard/editHazard.jsp");
			} else {
				hazardService.saveHazard(hazard);
				mav.setViewName("forward:/listHazards");
			}
		}
		return mav;
	}

	@RequestMapping("/hazardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
